package frame

import (
	uuid "github.com/satori/go.uuid"
	"time"

	"gorm.io/gorm"
)

type ModelUUID struct {
	Model
	UUID string `gorm:"column:uuid" json:"ID"`
}

// BeforeCreate ...
func (m *ModelUUID) BeforeCreate(tx *gorm.DB) error {
	if err := m.Model.BeforeCreate(tx); err != nil {
		return err
	}
	if m.UUID == "" {
		m.UUID = uuid.NewV4().String()
	}
	return nil
}

// Model ...
type Model struct {
	ID          int    `gorm:"column:id;primaryKey" json:"-"`
	CreatedTime string `gorm:"column:created_time"`
	UpdatedTime string `gorm:"column:updated_time"`
}

// BeforeCreate ...
func (m *Model) BeforeCreate(tx *gorm.DB) error {
	m.CreatedTime = time.Now().Format(TimeFormatTime)
	m.UpdatedTime = time.Now().Format(TimeFormatTime)
	return nil
}

// BeforeUpdate ...
func (m *Model) BeforeUpdate(tx *gorm.DB) error {
	m.UpdatedTime = time.Now().Format(TimeFormatTime)
	return nil
}

// SoftDelete ...
type SoftDelete struct {
	DeletedTime gorm.DeletedAt `gorm:"column:deleted_time" json:"-"`
}
